【Clojure入門】 Leiningenをインストールする
ClojureでもScalaと同様にプログラムを手動でコンパイルして実行することもできますが、通常はデファクトスタンダードのビルドツールLeiningen(一般的な読み方は「ライニンゲン」)を利用します。 Leiningen以外の選択肢としては特にClojureScriptを扱う場合に便利なBoot、Clojure公式提供のコマンドラインツールClojure CLIなどもあります。 Javaのインストール
Clojureの最新安定版1.10.1を利用する場合、Java 8以降をインストールしましょう。 macOSの場合
code:bash
$ brew install leiningen
Windowsの場合
code:bash
$ choco install lein
rebel-readlineの導入
Leiningenをインストールすると lein repl というコマンドでClojureのREPLが利用できるようになりますが、標準のREPLにはシンタックスハイライトや複数行編集機能などがないため、rebel-readlineというREPL機能を拡張するライブラリを利用するのがオススメです。 lein new app <任意のプロジェクト名> でClojureプロジェクトを生成し、
code:bash
$ lein new app clojure-playground
Generating a project called clojure-playground based on the 'app' template.
$ cd clojure-playground/
$ tree .
.
├── CHANGELOG.md
├── LICENSE
├── README.md
├── doc
│ └── intro.md
├── project.clj
├── resources
├── src
│ └── clojure_playground
│ └── core.clj
└── test
└── clojure_playground
└── core_test.clj
6 directories, 7 files
プロジェクト設定ファイル project.clj を例えば以下のように変更すると、
code:project.clj
(defproject clojure-playground "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:dependencies org.clojure/clojure "1.10.1" ; Clojure最新安定版1.10.1にする
:main ^:skip-aot clojure-playground.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}
:dev {:dependencies com.bhauman/rebel-readline "0.1.4" ; rebel というタスクを追加
lein rebel というコマンドでリッチなREPLが使えるようになります。
code:bash
$ lein rebel
user=>
REPLとLeiningen
Clojureでのプログラミングにおいては開発環境の一部としてREPL (read-eval-print loop)が非常に重要な役割を果たします。 ここでは、インストールしたLeiningenからREPLを起動してみましょう。
lein repl
ターミナルから先ほど作成したClojureプロジェクト内で lein repl コマンドを実行するとClojure REPLが起動します(※ lein repl はプロジェクト外で実行することも可能です)。
code:clojure
$ lein repl
nREPL server started on port 62759 on host 127.0.0.1 - nrepl://127.0.0.1:62759
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.1
OpenJDK 64-Bit Server VM 12.0.1+12
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
clojure-playground.core=>
操作方法説明の後に表示されている clojure-playground.core=> はREPLプロンプトで、 => の前にある clojure-playground.core が現在の名前空間(namespace)を表しています。
REPL内からClojureのバージョンを確認するには *clojure-version* と入力します。
code:clojure
clojure-playground.core=> *clojure-version*
{:major 1, :minor 10, :incremental 1, :qualifier nil}
project.cljに設定した通り、Clojure 1.10.1であることが分かります。
ここで *clojure-version* とは何なのか調べるには doc が利用できます。
doc は指定された名前に対応するドキュメンテーション文字列(docstring)を表示するマクロです。 (doc *clojure-version*) と入力すると以下のように表示されます。
code:clojure
clojure-playground.core=> (doc *clojure-version*)
-------------------------
clojure.core/*clojure-version*
The version info for Clojure core, as a map containing :major :minor
:incremental and :qualifier keys. Feature releases may increment
:minor and/or :major, bugfix releases will increment :incremental.
Possible values of :qualifier include "GA", "SNAPSHOT", "RC-x" "BETA-x"
nil
また、同様に source を利用するとソースコードを確認することができます。
code:clojure
clojure-playground.core=> (source *clojure-version*)
(def ^:dynamic *clojure-version*
(if (.contains version-string "SNAPSHOT")
(clojure.lang.RT/assoc clojure-version :interim true)
clojure-version))
nil
REPLから抜けるには、Control + Dまたは (exit), (quit) と入力します。
code:clojure
clojure-playground.core=> (exit)
Bye for now!
lein rebel
rebel-readlineを利用する場合には、先ほど設定した lein rebel コマンドを利用します(こちらは project.clj で独自に設定したコマンドなのでプロジェクト内でのみ利用可能です)。
code:clojure
$ lein rebel
user=>
rebel-readlineの操作方法を確認するには :repl/help と入力します。
code:clojure
user=> :repl/help
Current key map: :emacs
Clojure key bindings:
"TAB" clojure-indent-or-complete
"^X^A" clojure-apropos-at-point
"^X^D" clojure-doc-at-point
"^X^E" clojure-eval-at-point
"^X^I" clojure-indent-line
"^X^M" clojure-force-accept-line
"^X^S" clojure-source-at-point
Available Commands:
:repl/help
Prints the documentation for all available commands.
:repl/key-bindings
With an argument displays a search of the current key bindings
Without any arguments displays all the current key bindings
:repl/quit
Quits the REPL. This may only work in certain contexts.
:repl/set-color-theme
Change the color theme to one of the available themes:
(:dark-screen-theme :light-screen-theme)
:repl/set-key-map
Changes the key bindings to the given key-map. Choose from: (:emacs :vicmd :viins)
:repl/toggle-color
Toggle ANSI text coloration on and off.
:repl/toggle-completion
Toggle the completion functionality on and off.
:repl/toggle-eldoc
Toggle the auto display of function signatures on and off.
:repl/toggle-highlight
Toggle readline syntax highlighting on and off. See
:repl/toggle-color if you want to turn color off completely.
:repl/toggle-indent
Toggle the automatic indenting of Clojure code on and off.
lein repl で起動したREPLと異なり、rebel-readlineでは起動後に doc, source などのREPLで便利なユーティリティがデフォルトでは利用できないため、 (require '[clojure.repl :refer :all]) をあらかじめ実行しておくのがオススメです。
code:clojure
nil
REPLから抜けるには、Control + Dまたは :repl/quit と入力します。
code:clojure
user=> :repl/quit
Bye!
Leiningenのバージョンについて
Leiningenのバージョンを確認するには、ターミナルから lein version と入力します。
code:bash
$ lein version
Leiningen 2.9.1 on Java 12.0.1 OpenJDK 64-Bit Server VM